﻿/**
 * 弹出操作层 popDiv('url');return false;
 * @loadUrl 加载到弹出层作为内容的链接地址
 * @hash 给关闭的标签添加前页面的hash
 * @.pop_close 取消按钮，直接执行clearPop();清除弹出层
 */
function popDiv(loadUrl,hash,callback){
	$.ajax({
	  url: loadUrl,
	  cache: false,
	  dataType: "text",
	  async : false,
	  error: function(XmlHttpRequest, textStatus, errorThrown){
	  	popError("<div style='width:450px;'>加载页面出错，请进行刷新，然后再试一次。</div>","发生错误"); 
	  },
	  success: function(html){
	  	dialogHtml(html,"",hash);
	  },
	  complete: function(){
		positionDiv();
		if(callback!=undefined){
	  		eval(callback);
	  	}
	  }
	});
}

/**
 * 弹出层html的添加
 */
var dialogHtml = function(html,title,hash){
	clearPop();
	if(title!=null && title!=""){
		html="<div class='pop_content_title'>"+title+"</div><div class='pop_content_container'>"+html+"</div>";
	}
	hash!=undefined ? hash="#"+hash : hash="javascript:clearPop();";
	var popHtml=
		"<a href='"+hash+"' class='pop_bg'></a>" +
		"<table class='pop_dialog_table'>" +
		"	<tbody>" +
		"		<tr><td class='pop_topleft'></td><td class='pop_border'></td><td class='pop_topright'></td></tr>" +
		"		<tr><td class='pop_border'></td><td>" +
		"			<div class='pop_content_box'>" +
		"				<div><a href='"+hash+"' class='pop_close'>×</a></div>" +
		"				<div class='pop_content'>"+html+"</div>" +
		"			</div>" +
		"		</td><td class='pop_border'></td></tr>" +
		"		<tr><td class='pop_bottomleft'></td><td class='pop_border'></td><td class='pop_bottomright'></td></tr>" +
		"	</tbody>" +
		"</table>"
	$("body").append(popHtml);
	if($.browser.msie && $.browser.version == "6.0"){
		$(window).scroll(function() {
			positionDiv();
		});
	}
	$(window).resize(function(){
		positionDiv();
	});
}

/**
 * 弹出层的位置重新设置
 */
function positionDiv() {
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var thisWidth,thisHeight,topOffset,lefOffset,scrollTop=0;
	thisWidth = parseFloat($(".pop_dialog_table").width());
	thisHeight = parseFloat($(".pop_dialog_table").height());
	if($.browser.msie && $.browser.version == "6.0"){
		scrollTop=$(document).scrollTop();
	}
	$(".pop_dialog_table").css({top:windowHeight/2-thisHeight/2+scrollTop,left:windowWidth/2-thisWidth/2});
};

/**
 * 清除弹出层方法
 */
function clearPop() {
	$("table.pop_dialog_table,.pop_bg").remove();
}

/**
 * 弹出提示层 popRight('content');popError('content');
 * 继承popTips
 */
function popError(content,title){
	if(title==null){
		title="错误";
	}
	popDialog(content,title);
}
function popRight(content,title){
	if(title==null){
		title="正确";
	}
	popDialog(content,title);
}
function popDialog(content,title){
	dialogHtml(content,title);
	positionDiv();
}

/**
 * 	look.ui.pop 1.0 - jQuery plugin
 *	written by Fangjun Yu
 *
 *	Copyright (c) 2011 ZPCX (http://www.umu.so)
 *
 *	Built for CYGC
 *
 */
 
/**
 *	markup example for $.lookpop();
 *	
 *	@param {String} container 弹出交互层html的添加地
 *	@param {String} contentHtml 内容
 */

(function($) {
	$.lookpop = function(options){
 
		var defaults = {
			container: "body",
			left: 0,
			top: 0,
			contentHtml: "",
			ifRemove: true,
			addClass: "",
			remover: ".look_pop_panel"
		};
		var options = jQuery.extend(defaults, options);
		var panelHtml =
			'<div class="look_pop_panel '+options.addClass+'" style="top:'+options.top+'px;left:'+options.left+'px;">'+
			'	<div class="look_pop_panel_in">'+
			'		<a href="javascript:" class="look_pop_panel_closer">×</a>'+
			'		<div class="loop_pop_panel_content">'+options.contentHtml+'</div>'+
			'	</div>'+
			'</div>';
			
		if(options.ifRemove){
			$(options.remover).remove();
		}
		$(options.container).append(panelHtml);
		$(".look_pop_panel_closer").bind("click",function(){
			$(this).parents(".look_pop_panel").remove();
		});
	};
})(jQuery);